package com.decent.ejfadmin.utils;


import org.apache.tomcat.util.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;

/**
 * describe:DES工具类
 *
 * @author 陈豆豆
 * @date 2019/09/07
 */
@SuppressWarnings({"WeakerAccess", "StringBufferMayBeStringBuilder", "StringBufferReplaceableByString"})
public final class Des {
    private static final String ALGORITHM_DES = "DES/CBC/PKCS5Padding";
    private static String privateKey = "EJF110";

    /**
     * DES算法，加密
     *
     * @param data 待加密字符串
     * @param key  加密私钥，长度不能够小于8位
     * @return 加密后的字节数组，一般结合Base64编码使用
     * 异常
     */
    public static String encode(String key, String data) {
        try {
            return encode(privateKey + key, reverse(data).getBytes());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * DES算法，加密
     *
     * @param data 待加密字符串
     * @param key  加密私钥，长度不能够小于8位
     * @return 加密后的字节数组，一般结合Base64编码使用
     * 异常
     */
    public static String encode(String key, byte[] data) {
        try {
            DESKeySpec dks = new DESKeySpec((privateKey + key).getBytes());

            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            // key的长度不能够小于8位字节
            Key secretKey = keyFactory.generateSecret(dks);
            Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
            AlgorithmParameterSpec paramSpec = new IvParameterSpec("********".getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);

            byte[] bytes = cipher.doFinal(data);
            return Base64.encodeBase64String(bytes);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * DES算法，解密
     *
     * @param data 待解密字符串
     * @param key  解密私钥，长度不能够小于8位
     * @return 解密后的字节数组
     */
    public static byte[] decode(String key, byte[] data) {
        try {
            DESKeySpec dks = new DESKeySpec((privateKey + key).getBytes());
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            // key的长度不能够小于8位字节
            Key secretKey = keyFactory.generateSecret(dks);
            Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
            AlgorithmParameterSpec paramSpec = new IvParameterSpec("********".getBytes());
            cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
            return cipher.doFinal(data);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String reserveDecode(String key, String data) {
        try {
            byte[] bytes = Base64.decodeBase64(data);
//            byte[] bytes=data.getBytes();
            DESKeySpec dks = new DESKeySpec((key).getBytes());
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            // key的长度不能够小于8位字节
            Key secretKey = keyFactory.generateSecret(dks);
            Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
            AlgorithmParameterSpec paramSpec = new IvParameterSpec("12345678".getBytes());
            cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
            String temp = new String(cipher.doFinal(bytes));
            return new StringBuffer(temp).toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String encodeValue(String key, String data) throws Exception {
        DESKeySpec dks = new DESKeySpec((key).getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        // key的长度不能够小于8位字节
        Key secretKey = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
        AlgorithmParameterSpec paramSpec = new IvParameterSpec("12345678".getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
        byte[] bytes = cipher.doFinal(data.getBytes());
        return Base64.encodeBase64String(bytes);
    }


    /**
     * 获取编码后的值
     *
     * @param key  key
     * @param data 信息
     * @return str
     */
    public static String decodeValue(String key, String data) {
        byte[] datas;
        String value = "";

        datas = decode(privateKey + key, Base64.decodeBase64(data));

        if (datas != null) {
            value = new String(datas);
        }
        if ("".equals(value)) {
            return null;
        }
        return reverse(value);
    }

    public static String reverse(String string) {
        StringBuffer temp = new StringBuffer(string);
        return (temp.reverse()).toString();
    }

    public static void main(String[] args) throws Exception {
        String e = encodeValue("supplied", "asdf1234");
        System.out.println("e->" + e);
        String d = reserveDecode("supplied", "gRPNIE Kvr4z2gfiqi4HcQ==");
        System.out.println("d->" + d);
    }
}